--This is cl_init.lua

include( 'shared.lua' )
include( 'vgui/cl_playermodel.lua' )
include( 'vgui/cl_votemap.lua' )
include( 'vgui/cl_welcome.lua' )
include( 'cl_deathnotice.lua' )

LocalPlayer( ).LambModel = ""
LocalPlayer( ).CombModel = ""
LocalPlayer( ).RoundState = 0
LocalPlayer( ).RoundNumber = 0
LocalPlayer( ).RoundTime = 0
LocalPlayer( ).DelayTime = 0
LocalPlayer( ).MaxBallCount = 3
Maplist = {}
local winner_info = ""
DrawEOR = CurTime( )

function GM:Initialize( )
		--Fonts
	surface.CreateFont( "comic sans ms", 20, 550, true, true, "HudFont", true, true )
	surface.CreateFont( "comic sans ms", 30, 550, true, true, "HudFontBig", true, true )
	surface.CreateFont( "comic sans ms", 70, 550, true, true, "HudFontWinner", true, true )
		--Normal colors
	COLOR_YELLOW = Color( 255, 255, 0 )
	COLOR_BLUE = Color( 0, 0, 255 )
	COLOR_GREEN = Color( 0, 255, 0 )
	COLOR_MAGENTA = Color( 255, 0, 255 )
	COLOR_BACKGRAY = Color ( 100, 100, 100 )
		--All the transparency colors
	COLOR_THUDBLUE = Color( 48, 104, 182, 150 )

	LocalPlayer( ).Points = 0
	LocalPlayer( ).LambModel = ""
	LocalPlayer( ).CombModel = ""
	LocalPlayer( ).RoundState = 0
	LocalPlayer( ).RoundNumber = 0
	LocalPlayer( ).RoundTime = 0
	LocalPlayer( ).DelayTime = 0
	LocalPlayer( ).MaxBallCount = 3
	RunConsoleCommand("db_get_info", "")
end

function GM:Think( )
	if ( LocalPlayer( ).UpdateMoney ) then
		if ( LocalPlayer( ).UpdateMoney < CurTime( ) ) then
			RunConsoleCommand("db_get_info", "")
			LocalPlayer( ).UpdateMoney = CurTime( ) + 1
		end
	else
		LocalPlayer( ).UpdateMoney = CurTime( ) + 1
	end
end

--Updates player info
function CountDeMoney( mess )
	LocalPlayer( ).CombModel = mess:ReadString( )
	LocalPlayer( ).LambModel = mess:ReadString( )
end
-- this is where we hook it.
usermessage.Hook( "SendStats", CountDeMoney )

--Updates round status
function RoundStats( mess )
	LocalPlayer( ).RoundState = tonumber( mess:ReadString( ) )
	LocalPlayer( ).RoundNumber = tonumber( mess:ReadString( ) )
	LocalPlayer( ).RoundTime = tonumber( mess:ReadString( ) )
	LocalPlayer( ).DelayTime = tonumber( mess:ReadString( ) )
end

usermessage.Hook( "SendRoundStats", RoundStats )

--Triggers mapvoting
function Votemap( mess )
	Maplist = string.Explode(",", mess:ReadString( ) )
	print(string.Implode(",", Maplist))
	GAMEMODE:VoteMap()
end

usermessage.Hook( "vote_for_next_map", Votemap )

/*---------------------------------------------------------
   Name: gamemode:HUDShouldDraw( name )
   Desc: return true if we should draw the named element
---------------------------------------------------------*/
function GM:HUDShouldDraw( name )
	local ply = LocalPlayer( )
	if ( ply && ply:IsValid( ) ) then
		if ( ply:Team( ) != TEAM_SPECTATOR ) then
			if ( name == "CHudHealth" ) then return false end
			if ( name == "CHudSuitPower" ) then return false end
			if ( name == "CHudWeaponSelection" ) then return false end
			if ( name == "CHudAmmo" ) then return false end
			if ( name == "CHudSecondaryAmmo" ) then return false end
		end
	end
	return true
end

--Hud Stuff
function GM:HUDPaint( )
	local ply = LocalPlayer( )
	if (ply.RoundTime) then
		local text = ""
		GAMEMODE:HUDDrawTargetID( )
		GAMEMODE:DrawDeathNotice( 0.85, 0.04 )
		GAMEMODE:HUDEndOfRound( )
		--Draw the time left in current round
		draw.RoundedBox( 6, 0, 0, 165, 50, COLOR_THUDBLUE )
		draw.DrawText( "Time Left: " .. ply.RoundTime, "HudFont", 10, 5, COLOR_YELLOW, TEXT_ALIGN_LEFT )
		--Decide what round it is
		if ( ply.RoundState == 0 ) then
			text = ""
		elseif ( ply.RoundState == 1 ) then
			if (ply.RoundTime > 0) then
				text = "Warm Up"
			else
				text = "Waiting for players"
			end
		elseif ( ply.RoundState == 2 ) then
			text = "Round Initialized"
		elseif ( ply.RoundState == 3 ) then
			text = "Round Over"
		elseif ( ply.RoundState == 4 ) then
			text = "Vote Map"
		end
		if ( ply.DelayTime > 0 ) then
			text = "Delay: " .. ply.DelayTime
		end
		--Draw what round it is
		draw.DrawText( text, "HudFont", 10, 25, COLOR_YELLOW, TEXT_ALIGN_LEFT )
		--Draw the ammo count
		if ( ply && ply:IsValid( ) ) then
			if ( ply:Team() != TEAM_SPECTATOR ) then 
				draw.RoundedBox( 6, ScrW( ) - 65, ScrH( ) - 61, 60, 60, COLOR_THUDBLUE )
				draw.DrawText( "Balls", "HudFontBig", ScrW( ) - 60, ScrH( ) - 65, COLOR_YELLOW, TEXT_ALIGN_LEFT )
				draw.DrawText( ply:GetAmmoCount( "Grenade" ), "HudFontBig", ScrW( ) - 34, ScrH( ) - 36, COLOR_YELLOW, TEXT_ALIGN_CENTER )
			end
		end
	end
end

/*---------------------------------------------------------
   Name: gamemode:HUDPaintBackground( )
   Desc: Same as HUDPaint except drawn before
---------------------------------------------------------*/
function GM:HUDPaintBackground( )

end

--This draws the scores display for the end of round.
function GM:HUDEndOfRound( )
	--Draw end of round crap
	if ( DrawEOR > CurTime( ) ) then
		--END OF ROUND HUD
		--Scores display
		draw.RoundedBox( 6, ScrW( )/2 - 250 , ScrH( )/2 - 200, 500, 400, COLOR_BACKGRAY )
		--Combine side
		draw.RoundedBox( 6, ScrW( )/2 - 240, ScrH( )/2 - 190, 235, 380, COLOR_BLUE )
		draw.DrawText( "Combine Team", "HudFontBig", ScrW( )/2 - 117.5, ScrH( )/2 - 180, COLOR_YELLOW, TEXT_ALIGN_CENTER )
		draw.DrawText( "Wins: ".. tostring(team.GetScore(TEAM_COMBINE)), "HudFontBig", ScrW( )/2 - 117.5, ScrH( )/2 - 150, COLOR_YELLOW, TEXT_ALIGN_CENTER )
		--Lambda side
		draw.RoundedBox( 6, ScrW( )/2 + 5, ScrH( )/2 - 190, 235, 380, COLOR_GREEN )
		draw.DrawText( "Lambda Team", "HudFontBig", ScrW( )/2 + 117.5, ScrH( )/2 - 180, COLOR_MAGENTA, TEXT_ALIGN_CENTER )
		draw.DrawText( "Wins: " .. tostring(team.GetScore(TEAM_LAMBDA)), "HudFontBig", ScrW( )/2 + 117.5, ScrH( )/2 - 150, COLOR_MAGENTA, TEXT_ALIGN_CENTER )
		--Winner display
		draw.RoundedBox( 6, ScrW( )/2 - 150 , ScrH( )/2 - 100, 300, 200, COLOR_BACKGRAY )
		draw.RoundedBox( 6, ScrW( )/2 - 140 , ScrH( )/2 - 90, 280, 180, COLOR_THUDBLUE )
		draw.DrawText( "WINNER: " .. winner_info, "HudFontBig", ScrW( )/2, ScrH( )/2 - 70, COLOR_YELLOW, TEXT_ALIGN_CENTER )
		draw.DrawText( "", "HudFontWinner", ScrW( )/2, ScrH( )/2 - 20, COLOR_YELLOW, TEXT_ALIGN_CENTER )
	end
end


function BeginEndOfRound( mess )
	DrawEOR = CurTime( ) + tonumber( mess:ReadString( ) )
	winner_info = mess:ReadString( )
	
end

usermessage.Hook( "EndOfRound", BeginEndOfRound )

/*---------------------------------------------------------
   Name: CalcView
   Allows override of the default view
---------------------------------------------------------*/
function GM:CalcView( ply, origin, angles, fov, znear, zfar )
	if (GetConVar("db_fps_death"):GetInt() == 1) then
	// get their ragdoll
	local ragdoll = ply:GetRagdollEntity()
	if( !ragdoll || ragdoll == NULL || !ragdoll:IsValid() ) then return; end
	// find the eyes
	local eyes = ragdoll:GetAttachment( ragdoll:LookupAttachment( "eyes" ) )
	// setup our view
	local view = {
		origin = eyes.Pos,
		angles = eyes.Ang,
		fov = 90, 
	}
	return view
	
	else
	
	local Vehicle = ply:GetVehicle()
	local wep = ply:GetActiveWeapon()

	
	if ( ValidEntity( Vehicle ) && 
		 gmod_vehicle_viewmode:GetInt() == 1 &&
		 ply:ShouldDrawLocalPlayer() // don't go into thirdperson until we're done with the animation
		) then
		
		return GAMEMODE:CalcVehicleThirdPersonView( Vehicle, ply, origin*1, angles*1, fov, znear, zfar )
		
	end

	local ScriptedVehicle = ply:GetScriptedVehicle()
	if ( ValidEntity( ScriptedVehicle ) ) then
	
		// This code fucking sucks.
		local view = ScriptedVehicle.CalcView( ScriptedVehicle:GetTable(), ply, origin, angles, fov, znear, zfar )
		if ( view ) then return view end

	end

	local view = {}
	view.origin 	= origin
	view.angles		= angles
	view.fov 		= fov
	view.znear		= znear
	view.zfar		= zfar
	
	// Give the active weapon a go at changing the viewmodel position
	
	if ( ValidEntity( wep ) ) then
	
		local func = wep.GetViewModelPosition
		if ( func ) then
			view.vm_origin,  view.vm_angles = func( wep, origin*1, angles*1 ) // Note: *1 to copy the object so the child function can't edit it.
		end
		
		local func = wep.CalcView
		if ( func ) then
			view.origin, view.angles, view.fov = func( wep, ply, origin*1, angles*1, fov ) // Note: *1 to copy the object so the child function can't edit it.
		end
	
	end
	
	return view
	end
end

function explode( mess )
	local vPoint =  mess:ReadEntity( ):GetPos( )
	local effectdata = EffectData( )
	effectdata:SetStart( vPoint ) // not sure if we need a start and origin (endpoint) for this effect
	effectdata:SetOrigin( vPoint )
	effectdata:SetScale( 1 )
	util.Effect( "crapy_star_effect", effectdata )	
end
usermessage.Hook( "player_explode", explode )